package com.koron.dynamic.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * @ClassName RedisPubSubConfig
 * @description
 * @Author zhouj
 * @Date 2021/10/20 16:14
 */

/**
 * 订阅配置 channel-dynamicDs是频道，可以多个
 * 也可以patternToppic
 */
@Configuration
public class RedisPubSubConfig {

    @Bean
    MessageListenerAdapter messageListener() {
        return new MessageListenerAdapter(new KrSubcribe());
    }

    @Bean
    RedisMessageListenerContainer redisContainer(RedisConnectionFactory factory) {
        final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(messageListener(), new ChannelTopic("channel-dynamicDs"));
        return container;
    }
}
